<?php
/*------------------------------------------------------------------------
 * Login.php 
 *
 * 登录控制器
 * 	
 * Created on 2017-01-05
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2017 All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace app\index\controller;

class Login extends Base{

	public function index(){
		$jumpurl = session('login_jumpurl');
		if(!$jumpurl){
			session('login_jumpurl', $_SERVER['HTTP_REFERER']);
		}

		$this->add_step('登录');
		if($this->isPost()){
			$username = $this->_post('username', '账号不能为空！');
			$password = $this->_post('password', '密码不能为空！');
			$model = model('User');
			$user_info = $model->login($username, $password);
			if($user_info){
				session('login_jumpurl', null);
				$model->save_session($user_info);
				$jumpurl = $jumpurl ? $jumpurl : U('/');
				$this->success('登录成功！', $jumpurl);
			}
			$this->error($model->tips_info);
		}
		return $this->view();
	}
	// 微信二维码
	public function weixin_login_qrcode(){
		$scan_type = $this->_get('scan_type', '', 'login');
		$model = model('Weixin');

		// 获取微信推广二维码URL地址
		$app_type = 'web';
		$params['app_type']   = $app_type;
		$params['session_id'] = session_id();
		$content = $model->get_qrcode_url($scan_type, $this->_user_id, 0, $params, $app_type);
		session('scene_id', $model->scene_id);

		import('QRcode', EXTEND_PATH, '.class.php');
		\QRcode::png($content, false, QR_ECLEVEL_L, 12, 0);
	}
	// 微信扫描登录状态
	public function weixin_login_status(){
		$scene_id = session('scene_id');
		$jumpurl  = session('login_jumpurl');
		if(!$scene_id){
			$this->error('对不起，您的操作有误！');
		}
		$model = model('Weixin');
		$info = $model->get_scene_id_info($scene_id);
		$user_id = $info['scan_user_id'];
		if($user_id){
			$user_model = model('User');
			session('login_jumpurl', null);
			db('WeixinQrcode')->where(array('scene_id' => $scene_id))->update(array('scan_user_id' => 0));
			// 获取用户信息
			$user_info = $user_model->user_info($user_id);
			//判断是否禁用
			if($user_info['status'] == 0){
				$this->error('您的账号被禁用，请联系相关人员！', '', array('is_tips' => 1));
			}
			// 更新用户登录TOKEN
			$user_info['token'] = $user_model->make_token($user_id);
			$user_model->save_session($user_info);
			$jumpurl = $jumpurl ? $jumpurl : U('/');
			$this->success('登录成功！', $jumpurl);
		}else{
			$this->error('您尚未扫描登录！');
		}
	}

	public function win_qrcode_login(){
		$type    = $this->_get('type', '', 'login');
		$jumpurl = session('login_jumpurl');
		if(!$jumpurl){
			session('login_jumpurl', $_SERVER['HTTP_REFERER']);
		}

		return $this->view();
	}
	// 微信登录回调
	public function weixin_callback(){
		$weixin_code = $this->_get('code');
		if($weixin_code){
			import("WeixinWebOAuth", '../Common/Class/Weixin/');
			$weixin = new \WeixinWebOAuth();
			$token_openid = $weixin->get_token_openid($weixin_code);
			if(!$token_openid['token']){
				$this->error('微信登录超时，请重新登录！');
			}

			$weixin_user = $weixin->get_user_info($token_openid['token'], $token_openid['openid']);
			$unionid  = $weixin_user['unionid'];
			$nickname = $weixin_user['nickname'];
			$head_url = $weixin_user['headimgurl'];
			if(!$unionid){
				$this->error('微信登录超时，请重新登录！');
			}
			// 微信登录成功操作
			$jumpurl = session('login_jumpurl');
			// 头像URL
			D('User')->head_url = $head_url;
			if($user_info = D('User')->sso_login('weixin', $unionid, $nickname, true)){
				session('login_jumpurl', null);
				D('User')->save_session($user_info);
				$jumpurl = $jumpurl ? $jumpurl : U('/');
				$this->success('登录成功！', $jumpurl);
			}
			$this->error(D('User')->tips_info);
		}
	}
	// 微信绑定回调
	public function weixin_bind_callback(){
		$weixin_code = $this->_get('code');
		$user_id     = $this->user_id();
		if($weixin_code){
			import("WeixinWebOAuth", '../Common/Class/Weixin/');
			$weixin = new \WeixinWebOAuth();
			$token_openid = $weixin->get_token_openid($weixin_code);
			if(!$token_openid['token']){
				$this->error('微信登录超时，请重新登录！');
			}

			$weixin_user = $weixin->get_user_info($token_openid['token'], $token_openid['openid']);
			$unionid  = $weixin_user['unionid'];
			$nickname = $weixin_user['nickname'];
			if(!$unionid){
				$this->error('微信登录超时，请重新登录！');
			}
			// 成功操作
			if($user_info = D('User')->sso_bind('weixin', $user_id, $unionid, $nickname)){
				$jumpurl = $_SERVER['HTTP_REFERER'];
				$jumpurl = $jumpurl ? $jumpurl : U('/');
				$this->success('绑定成功！', $jumpurl);
			}
			$this->error(D('User')->tips_info);
		}
	}
}